package myschool.web;

import myschool.entity.Grade;
import myschool.entity.Page;
import myschool.service.GradeService;
import myschool.util.StrUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//@WebServlet(name = "GradeServlet",value = "/grade")
public class GradeServlet extends HttpServlet {
    public GradeServlet(){
        System.out.println("Gradeservlet加载了！！！！");
    }
    String enc;
    @Override
    public void init(ServletConfig config) throws ServletException {
        enc=config.getInitParameter("enc");//初始化参数
        System.out.println("初始化完成了。。。");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doDelete(req, resp);
    }

    @Override
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doOptions(req, resp);
    }

    //服务
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("service method："+req.getMethod());
        super.service(req, resp);//不能少
    }

    //销毁
    @Override
    public void destroy() {
        System.out.println("GradeServlet销毁了");
    }
    //get和post行为一致
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入了Get方法：：。。。。");
        response.setContentType("text/html;charset="+this.enc);
        Page<Grade> page = GradeService.ME.page(StrUtil.toInt(request.getParameter("pageNo"),1), Page.PAGESIZE);

        request.setAttribute("p",page);
        request.getRequestDispatcher("/WEB-INF/jsps/grades.jsp").forward(request,response);
        //        PrintWriter out = response.getWriter();
//        out.print("<html>\n" +
//                "<head>\n" +
//                "    <title>Title</title>\n" +
//                "    <style>\n" +
//                "        .hide {\n" +
//                "            pointer-events: none;\n" +
//                "            color: gray;\n" +
//                "        }\n" +
//                "    </style>\n" +
//                "    <script src=\"js/jquery-3.6.3.min.js\"></script>\n" +
//                "</head>\n" +
//                "<body>");
//        out.print("<table border='1' cellpadding=\"0\" cellspacing=\"0\">");
//        out.print("<tr><th>编号</th><th>年级名</th></tr>");
//        for(Grade g:page.getData()){
//            out.print("<tr><td>"+g.getId()+"</td><td>"+g.getGname()+"</td></tr>");
//        }
////        out.print("</table>");
        //response.getWriter().print("你好，这是servlet...get");

    }
}
